//
//  BDShopJSCell.m
//  iOS_beboy
//
//  Created by wen on 2017/9/23.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopJSCell.h"
#import "BDShopJSModel.h"

@interface BDShopJSCell ()

@property (nonatomic, strong) UIImageView *haveUserImageView;
@property (nonatomic, strong) UIView *tagsView;

@property (nonatomic, copy) void(^avatarClick)();

@end

@implementation BDShopJSCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    style = UITableViewCellStyleValue1;
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _haveUserImageView = [[UIImageView alloc] init];
        _haveUserImageView.image = [UIImage imageNamed:@"shop_JS_dongtai"];
        _haveUserImageView.size = CGSizeMake(15, 15);
        [self.contentView addSubview:_haveUserImageView];
        _tagsView = [[UIView alloc] init];
        [self.contentView addSubview:_tagsView];
        
        _topLine = [[UIView alloc] init];
        _topLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        [self.contentView addSubview:_topLine];
        
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        [self.contentView addSubview:_bottomLine];
    }
    return self;
}

- (void)configeWithModel:(id)model avatarClick:(void(^)())avatarClick{
    BDShopJSModel *JS = model;
    _avatarClick = avatarClick;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:JS.cover] placeholderImage:[UIImage imageNamed:@"shop_JS_moren"]];
    
    self.textLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%zd号"), JS.no];
    self.detailTextLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%zd岁 | %zdcm | %zdkg | %zd/%zd/%zd"), JS.age, JS.height, JS.weight, JS.bust, JS.waistline, JS.hips];
    
    if (JS.user_id == 0) {
        _haveUserImageView.hidden = YES;
    }else{
        _haveUserImageView.hidden = NO;
    }
    
    CGFloat labelWidth = 65;
    CGFloat labelHeight = 26;
    CGFloat labelSpace = 10;
    CGFloat maxWidth = 0;
    for (int i = 0; i < JS.tags.count; i++) {
        UILabel *label = [_tagsView viewWithTag:5555 + i];
        if (label == nil) {
            label = [[UILabel alloc] init];
            label.tag = 5555 + i;
            label.font = [UIFont systemFontOfSize:13];
            label.textAlignment = NSTextAlignmentCenter;
            label.layer.masksToBounds = YES;
            label.layer.cornerRadius = 3;
            [_tagsView addSubview:label];
        }
        BDTagsModel *tag = JS.tags[i];
        label.text = tag.name;
        label.backgroundColor = [UIColor colorWithHexString:tag.bg_color];
        label.textColor = [UIColor colorWithHexString:tag.text_color];
        label.size = CGSizeMake(labelWidth, labelHeight);
        label.left = (labelSpace + labelWidth) * i;
        
        maxWidth = label.right;
    }
    _tagsView.size = CGSizeMake(maxWidth, labelHeight);
}

- (void)layoutSubviews{
    self.imageView.size = CGSizeMake(50, 50);
    self.imageView.left = 15;
    self.imageView.top = 18;
    
    self.textLabel.font = [UIFont systemFontOfSize:20];
    self.textLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    [self.textLabel sizeToFit];
    self.textLabel.left = self.imageView.right + 10;
    self.textLabel.top = self.imageView.top;
    
    _haveUserImageView.left = self.textLabel.right + 10;
    _haveUserImageView.centerY = self.textLabel.centerY;
    
    self.detailTextLabel.font = [UIFont systemFontOfSize:13];
    self.detailTextLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    [self.detailTextLabel sizeToFit];
    self.detailTextLabel.left = self.imageView.right + 10;
    self.detailTextLabel.top = self.textLabel.bottom + 10;
    
    _tagsView.left = self.textLabel.left;
    _tagsView.top = self.detailTextLabel.bottom + 10;
    
    _topLine.width = self.width;
    _topLine.height = 0.75;
    _topLine.top = 0;
    _topLine.left = 0;
    
    _bottomLine.width = self.width;
    _bottomLine.height = 0.75;
    _bottomLine.bottom = self.height;
    _bottomLine.left = 0;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:self.contentView];
    if (CGRectContainsPoint(self.imageView.frame, point)) {
        if (_avatarClick) {
            _avatarClick();
        }
    }
}

@end
